import { arrHasData } from '/@/utils';
import { ElMessage } from 'element-plus';

export const useDel = (api, callback, text) => {
	const loading = ref(false);
	const delItem = async (id) => {
		loading.value = true;
		let res = await api(id);
		loading.value = false;
		if (res.code !== 200) return;
		ElMessage.success(text || '删除成功！');
		callback();
	};
	return {
		loading,
		delItem,
	};
};

export const useApi = (api) => {
	const loading = ref(false);
	const request = async (...args) => {
		try {
			loading.value = true;
			let res = await api(...args);
			if (process.env.NODE_ENV === 'development') {
				setTimeout(() => {
					loading.value = false;
				}, 500);
			} else {
				loading.value = false;
			}
			return res;
		} catch (error) {
			console.log(error);
			loading.value = false;
		}
	};
	return {
		loading,
		request,
	};
};

export const useSinglePic = (ruleForm) => {
	const isShowPicSelector = ref(false);

	return {
		isShowPicSelector,
		selectPic() {
			isShowPicSelector.value = true;
		},
		getPic(pic) {
			if (!arrHasData(pic)) return;
			ruleForm.value.icon = pic[0].imgUrl;
		},
	};
};
/* 
    @param string 父页面路由名称
 */
export const useShowHome = (name) => {
	const route = useRoute();
	return {
		isShowHome: computed(() => {
			return route.name === name;
		}),
	};
};
